package test.service;

import test.testcase.TestCaseApi;
import test.testcase.dao.TestCaseDao;
import test.testcase.dao.TestModelDao;
import test.utils.JsonResult;
import test.utils.Mysql;

public class ServiceTestCaseApi {
	
	public static TestCaseApi geTestCaseApi(String id) {
		TestCaseApi testCaseApi = new TestCaseApi();
		JsonResult result = TestCaseDao.geTestCaseApi(id);
		testCaseApi.setId(id);
		testCaseApi.setMethod(result.read("data[0].method").toString());
		testCaseApi.setUrl(result.read("data[0].url").toString());
		testCaseApi.setStatus(result.read("data[0].status").toString());
		testCaseApi.setContenttype(result.read("data[0].contenttype").toString());
		testCaseApi.setContenttypeid(result.read("data[0].contenttypeid").toString());
		testCaseApi.setInserttime(result.read("data[0].inserttime").toString());
		testCaseApi.setUpdatetime(result.read("data[0].updatetime").toString());
		testCaseApi.setContent(result.read("data[0].content").toString());
		testCaseApi.setUserid(result.read("data[0].userid").toString());
		testCaseApi.setUsername(result.read("data[0].username").toString());
		testCaseApi.setProjectname(result.read("data[0].projectname").toString());
		testCaseApi.setProjectid(result.read("data[0].projectid").toString());
		return testCaseApi;
	}
	
	public static boolean getApiUsedCount(String apiid) {
		JsonResult result = TestCaseDao.getApiUsedCount(apiid);
		if (result.read("data[0].count").toInt() >0 ) {
			return true;
		}else {
			result = TestModelDao.getApiUsedCount(apiid);
			if (result.read("data[0].count").toInt() >0 ) {
				return true;
			}
		}
		return false;
	}
	
	public static JsonResult delApi(String id) {
		return Mysql.excute("delete from api where id="+id);
	}

}
